package com.project.controller.web;

import com.project.common.utils.Constants;
import com.project.entity.SchoolManagerInfo;
import com.project.entity.User;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.myBeans.SchoolManagerDetail;
import com.project.service.SchoolManagerInfoService;
import com.project.service.SchoolMangerService;
import com.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 17:05
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/schoolmanager")
@Api(description = "学院管理员相关",value = "schoolmanager")
public class SchoolManagerInfoController {
    @Autowired
    private SchoolManagerInfoService schoolManagerInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private SchoolMangerService schoolMangerService;
    /**
     * TateBrown 2018年5月27日
     *
     * @param userId
     * @return
     */
    @ApiOperation("根据userId查找学院管理员信息")
    @GetMapping("/findByUserId/{userId}")
    public R findByUserId(@PathVariable("userId") Integer userId) {
        try {
            return R.ok("获取成功").put("data",schoolManagerInfoService.findByUserId(userId));
        } catch (Exception e) {
          return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param schoolManagerDetail
     * @return
     */
    @ApiOperation("创建学院管理员账号")
    @PostMapping("/addAccount")
    public R addAccount(@RequestBody SchoolManagerDetail schoolManagerDetail) {
        try {
            User user = schoolManagerDetail.getUser();
            SchoolManagerInfo schoolManagerInfo = schoolManagerDetail.getSchoolManagerInfo();

            if (schoolManagerInfo.getSchoolid() == null) {
                return R.error("未选择学院");
            } else { //先检查该学院是否已存在管理员
                if (schoolManagerInfoService.checkSchoolManagerIsExist(schoolManagerInfo.getSchoolid())) {
                   return R.error("学院已存在管理员");
                } else if (user != null) { //检查用户名是否已存在
                    if (user.getUsername() != null)
                        if (userService.checkUsernameAndUniversityIdIsExist(user.getUsername(), user.getUniversityid())) {
                            return R.error("用户名已存在");
                        } else {
                            //设置用户类型
                            user.setUsertype(Constants.USERTYPE.SCHOOL.value());
                            //创建后的用户自动启用 不需要审核
                            user.setState(Constants.USERSTATE.PASS.value());
                            schoolManagerDetail.setUser(user);
                            schoolMangerService.addAccount(schoolManagerDetail);
                        }
                } else {
                  return R.error("用户基本信息不存在");
                }
                  return R.ok();
            }

        } catch (Exception e) {
            e.printStackTrace();
          return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param userId
     * @return
     */
    @ApiOperation("根据用户表id删除学院管理员")
    @GetMapping("/removeOneByUserId/{userId}")
    public R removeOneByUserId(@PathVariable("userId") Integer userId){
        try {
            schoolMangerService.removeOneByUesrId(userId);
           return R.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
           return R.error("程序异常");
        }
    }

}
